package com.yaozheng.vocationaltraining.service.impl;

import java.util.HashMap;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.iview.IVideoCoursewareOperatingView;
import com.yaozheng.vocationaltraining.iview.IVideoCoursewareView;
import com.yaozheng.vocationaltraining.service.CoursewareDetailOperatingService;
import com.yaozheng.vocationaltraining.service.CoursewareDetailService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

/**
 * 课件详情
 *
 * @author Administrator
 */
@EBean
public class CoursewareDetailOperatingServiceImpl implements
        CoursewareDetailOperatingService {
    IVideoCoursewareOperatingView iTokenBaseView;

    @Override
    public void init(IVideoCoursewareOperatingView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Background
    @Override
    public void saveProgress(int id, int progress) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("id", String.valueOf(id));
            para.put("progress", String.valueOf(progress));
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/course/progress",
                    iTokenBaseView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iTokenBaseView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.saveProgressSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.saveProgressError(error);
                        }

                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .saveProgressError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void submitServerDownloadSuccess(int id) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("id", String.valueOf(id));
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/course/download",
                    iTokenBaseView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iTokenBaseView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView
                                    .submitServerDownloadSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.submitServerDownloadError(error);
                        }

                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .saveProgressError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

}
